/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans;

import Utilities.Functions;
import java.sql.Timestamp;
import java.io.Serializable;
import Utilities.Interfaces.SelfValidating;

/**
 * This class is used for storing user data when writing it to or retrieving it
 * from the database. <br><br>
 *
 * <b>Note:</b> <i>Passwords are not stored when the object is built with
 *              information from the database.</i>
 *
 * @author Angel Sanadinov
 */
public class UserBean implements Serializable, SelfValidating
{
    private int userId;             //id associated with the user *
    private String username;        //user's name
    private String password;        //user's password **
    private Timestamp dateCreated;  //date on which the user was added in the database *
    private Timestamp dateLastLogin;//last date on which the user has logged in *
    private int accountLevel;       //account status
    private String lastLoginAddress;//user IP address at last login
    //* only available when object is built with data from the database
    //** not available when object is built with data from the database

    /**
     * Constructs a new object, most often used when user data is to be inserted in
     * the database.
     *
     * @param username the name of the user
     * @param password the user's password
     * @param accountLevel the account level of the new user
     */
    public UserBean(String username, String password, int accountLevel)
    {
        this.username = username;
        this.password = password;
        this.accountLevel = accountLevel;
    }

    /**
     * Constructs a new object, most often used when user data is to be retrieved from
     * the database.
     *
     * @param userId the id of the user
     * @param username the name of the user
     * @param created the date on which the user was created
     * @param lastLogin the date on which the user last logged in
     * @param accountLevel the account level of the user
     * @param lastLoginAddress the user's IP address at the last login
     */
    public UserBean(int userId, String username, Timestamp created, Timestamp lastLogin, 
                    int accountLevel, String lastLoginAddress)
    {
        this.userId = userId;
        this.username = username;
        this.dateCreated = created;
        this.dateLastLogin = lastLogin;
        this.accountLevel = accountLevel;
        this.lastLoginAddress = lastLoginAddress;
        this.password = null;
    }

    /**
     * Checks the validity of the object. <br><br>
     *
     * This object is valid only when built using data from the database.
     *
     * @return <code>true</code> if the object is valid or <code>false</code> otherwise
     */
    @Override
    public boolean isValid()
    {
        if(Functions.isUserIdValid(userId) && Functions.isTimestampValid(dateCreated)
                && username != null && password == null)
            return true;
        else
            return false;
    }

    /**
     * Retrieves the id of the user.
     *
     * @return the user id
     */
    public int getUserId()
    {
        return userId;
    }

    /**
     * Retrieves the name of the user.
     *
     * @return the user's name
     */
    public String getUsername()
    {
        return username;
    }

    /**
     * Retrieves the user's password.
     *
     * @return the user's password
     */
    public String getPassword()
    {
        return password;
    }

    /**
     * Retrieves a copy of the date on which the user was added in the database.
     *
     * @return the creation date
     */
    public Timestamp getDateCreated()
    {
        return (Timestamp)dateCreated.clone();
    }

    /**
     * Retrieves a copy of the last date on which the user logged in.
     *
     * @return the last login date
     */
    public Timestamp getDateLastLogin()
    {
        if(dateLastLogin != null)
            return (Timestamp)dateLastLogin.clone();
        else
            return null;
    }

    /**
     * Retrieves the account level of the user. <br><br>
     *
     * 0 -> account disabled <br>
     * 1 -> normal account <br>
     * 2 -> manager account <br>
     * 3 -> owner account
     *
     * @return the user's account level
     */
    public int getAccountLevel()
    {
        return accountLevel;
    }

    /**
     * Retrieves the user's last login IP address.
     *
     * @return the IP address
     */
    public String getLastLoginAddress()
    {
        return lastLoginAddress;
    }
}
